package com.study.demo.springmvc.advice;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.alibaba.fastjson.support.spring.annotation.FastJsonView;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 *
 * @author: elvin
 * @time: 2019-05-08 13:58
 * @desc:
 **/
@ControllerAdvice
public class ResponseBodyDataAdvice implements ResponseBodyAdvice<String> {
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        //boolean assignableFrom = FastJsonHttpMessageConverter.class.isAssignableFrom(converterType);
        //if(returnType.getClass().isAssignableFrom(HandlerMethod.ReturnValueMethodParameter.class))
        if(returnType.getGenericParameterType().equals(String.class)){
            return true;
        }
        return false;
    }

    @Override
    public String beforeBodyWrite(String body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        return body + "<br/> haha, this is bean modified ! ";
    }
}
